package com.penghaonan.appframework.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;

import com.penghaonan.appframework.reporter.Reporter;
import com.penghaonan.appframework.utils.threadpool.AThreadPool;
import com.penghaonan.appframework.utils.threadpool.IOThreadPool;
import com.penghaonan.appframework.utils.threadpool.IThreadPoolHolder;

abstract public class BaseFrameworkFragment extends Fragment implements IThreadPoolHolder {
    private IOThreadPool ioThreadPool;
    private OnBackPressedCallback backPressedCallback = new OnBackPressedCallback(false) {
        @Override
        public void handleOnBackPressed() {
            onBackPressed();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getLayoutRes() != 0) {
            return inflater.inflate(getLayoutRes(), container, false);
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        requireActivity().getOnBackPressedDispatcher().addCallback(this.getViewLifecycleOwner(), backPressedCallback);

        initViews(view);
        initDatas();
    }

    @Override
    public void onResume() {
        super.onResume();
        Reporter.getInstance().onFragmentResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        Reporter.getInstance().onFragmentPause(this);
    }

    @Override
    public void onDestroyView() {
        backPressedCallback.remove();
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (ioThreadPool != null) {
            ioThreadPool.stop(true);
        }
    }

    protected <T extends View> T findViewById(int id) {
        if (getView() == null) {
            return null;
        }
        return getView().findViewById(id);
    }

    protected void onBackPressed() {
    }

    protected void setHandleBackPress(boolean enable) {
        backPressedCallback.setEnabled(enable);
    }

    abstract public int getLayoutRes();

    abstract public void initViews(View root);

    abstract public void initDatas();

    protected ActionBar getActionBar() {
        Activity activity = getActivity();
        if (activity instanceof AppCompatActivity) {
            return ((AppCompatActivity) activity).getSupportActionBar();
        }
        return null;
    }

    /**
     * 用于页面展示上报
     */
    public String getFragmentName() {
        return getClass().getSimpleName();
    }

    public BaseFrameworkActivity getBaseActivity() {
        FragmentActivity activity = getActivity();
        if (activity instanceof BaseFrameworkActivity) {
            return (BaseFrameworkActivity) activity;
        }
        return null;
    }

    public NavController parentNavController() {
        Fragment parentFragment = getParentFragment();
        if (parentFragment == null) {
            return null;
        } else {
            return NavHostFragment.findNavController(getParentFragment());
        }
    }

    @Override
    public AThreadPool getIOThreadPool() {
        if (ioThreadPool == null) {
            ioThreadPool = new IOThreadPool();
        }
        return ioThreadPool;
    }
}
